<?php

namespace app\admin\model;

use think\Model;


class Project extends Model
{

    // 表名
    protected $name = 'project_new';

    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'carrier_id_relation_unit',
        'progress_status_time_text',
    ];

    public function __construct($data = [],$year='')
    {
        // $year = $year ?: date('Y');
        // $this -> name = 'project_'.$year;
        parent::__construct($data);
    }


    protected static function init()
    {
        Project::event('before_insert',function ($data){
            $data -> updateRelaByAdmin($data);
        });
        // Project::event('before_update',function ($data){
        //     $data -> updateRelaByAdmin($data);
        // });
    }


    protected function updateRelaByAdmin($data){
        
        $depKey = 'chargedep_ids' ;
        $depNamesKey = 'chargedep_names';
        if(isset($data[$depKey]) && $data[$depKey]){
            // $data[$depKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$depKey])->value('GROUP_CONCAT(id)');
            $data[$depNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$depKey])->value('GROUP_CONCAT(name)');
        }



        $assistKey = 'assist_ids' ;
        $assistNamesKey = 'assist_names';
        if(isset($data[$assistKey]) && $data[$assistKey]){
            $data[$assistKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$assistKey])->value('GROUP_CONCAT(id)');
            $data[$assistNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$assistKey])->value('GROUP_CONCAT(name)');
        }

        $sourceKey = 'project_source_ids';
        $sourceNamesKey = 'project_source_names';
        if(isset($data[$sourceKey]) && $data[$sourceKey]){
            $data[$sourceKey] = (new \app\admin\model\Activity())->whereIn('id', $data[$sourceKey])->value('GROUP_CONCAT(id)');
            $data[$sourceNamesKey] = (new \app\admin\model\Activity())->whereIn('id', $data[$sourceKey])->value('GROUP_CONCAT(name)');
        }

        $tradeKey = 'trade_category_ids';
        $tradeNamesKey = 'trade_category_names';
        if(isset($data[$tradeKey]) && $data[$tradeKey]){
            $data[$tradeKey] = (new \app\admin\model\TradeCategory())->whereIn('id', $data[$tradeKey])->value('GROUP_CONCAT(id)');
            $data[$tradeNamesKey] = (new \app\admin\model\TradeCategory())->whereIn('id', $data[$tradeKey])->value('GROUP_CONCAT(name)');
        }

        $settleKey = 'settle_street_ids';
        $settleNamesKey = 'settle_street_names';
        if(isset($data[$settleKey]) && $data[$settleKey]){
            $settle_street_ids = (new \app\admin\model\Chargedep())->whereIn('id', $data[$settleKey])->value('GROUP_CONCAT(id)');
            if(empty($settle_street_ids))
                $data[$settleKey] = '-100';
            else
                $data[$settleKey] = $settle_street_ids ;

            $settle_street_names = (new \app\admin\model\Chargedep())->whereIn('id', $data[$settleKey])->value('GROUP_CONCAT(name)');
            if(empty($settle_street_names))
                $data[$settleNamesKey] = '待定';
            else
                $data[$settleNamesKey] = $settle_street_names;
            
        }


        $industryKey = 'industry_category_id';
        $industryNamesKey = 'industry_category_name';
        if(isset($data[$industryKey]) && $data[$industryKey]){
            $data[$industryNamesKey] = (new \app\admin\model\IndustryCategory())->whereIn('id', $data[$industryKey])->value('GROUP_CONCAT(name)');
        }

        $realmKey = 'industry_realm_id';
        $realmNamesKey = 'industry_realm_name';
        if(isset($data[$realmKey]) && $data[$realmKey]){
            $data[$realmNamesKey] = (new \app\admin\model\Realm())->whereIn('id', $data[$realmKey])->value('GROUP_CONCAT(name)');
        }
        
        $fiveKey = 'five_industry_id';
        $fiveNamesKey = 'five_industry_name';
        if(isset($data[$fiveKey]) && $data[$fiveKey]){
            $data[$fiveNamesKey] = (new \app\admin\model\FiveIndustry())->whereIn('id', $data[$fiveKey])->value('GROUP_CONCAT(name)');
        }
            
        $leaderKey = 'branch_leader_id';
        $leaderNamesKey = 'branch_leader_name';
        if(isset($data[$leaderKey]) && $data[$leaderKey]){
            $data[$leaderNamesKey] = (new \app\admin\model\Leaders())->whereIn('id', $data[$leaderKey])->value('GROUP_CONCAT(name)');
        }

        if(isset($data['asset_type_id']))
        {
            $data['asset_type_name'] = config('app.asset_type_id')[$data['asset_type_id']] ?? '';
        }

        $carrierKey = 'carrier_id';
        $carrierNamesKey = 'carrier_name';
        if(isset($data[$carrierKey]) && $data[$carrierKey]){
            $data[$carrierNamesKey] = (new \app\admin\model\Carrier())->whereIn('id', $data[$carrierKey])->value('GROUP_CONCAT(name)');
        }
        
        // $departKey = 'organizer_departid';
        // $departNamesKey = 'organizer_depart';
        // if(isset($data[$departKey]) && $data[$departKey]){
        //     $data[$departNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$departKey])->value('GROUP_CONCAT(name)');
        // }

        // $mentKey = 'socialized_department_id';
        // $mentNamesKey = 'socialized_department';
        // if(isset($data[$mentKey]) && $data[$mentKey]){
        //     $data[$mentNamesKey] = (new \app\admin\model\Centraloffice())->whereIn('id', $data[$mentKey])->value('GROUP_CONCAT(name)');
        // }

        $leadersKey = 'leader_ids' ;
        $leadersNamesKey = 'leader_names';
        if(isset($data[$leadersKey]) && $data[$leadersKey]){
            $data[$leadersKey] = (new \app\admin\model\ContactLeaders())->whereIn('id', $data[$leadersKey])->value('GROUP_CONCAT(id)');
            $data[$leadersNamesKey] = (new \app\admin\model\ContactLeaders())->whereIn('id', $data[$leadersKey])->value('GROUP_CONCAT(name)');
        }

    }



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getProgressStatusTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['progress_status_time']) ? $data['progress_status_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

   

    public function getCarrierIdRelationUnitAttr($value, $data)
    {
        $value = $data['carrier_id'];
        $relation_unit= '';
        switch ($value) {
            case '1':
            case '6':
                $relation_unit = '亩';
                break;
            case '2':
            case '3':
            case '4':
                $relation_unit = '平米';
                break;
            default:
                $relation_unit = '';
                break;
        }
        return $relation_unit;
    }
    public function managers() {
        return $this->hasMany('AdminProject','project_id');
    }

    public function stages()
    {
        return $this->hasMany('ProjectStage','project_id');
    }


}
